/*
The MIT License (MIT)

Copyright (c) 2013 Mike Dapiran, Brian May, Richard Pospesel, and Bert Wierenga

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software 
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#pragma once
#include "Hogshead.Common.h"
#include "Hogshead.Core.h"

#include "../DataStructures/ConstantBuffer/ConstantBufferGroup.h"
#include "../Content/Technique.h"

#include "../Directors/IProcessInfo.h"

struct ID3D11Buffer;

namespace Hogshead
{
	namespace DX11Rendering
	{
		struct LightingInfo;
		class MeshFilter;

		/**
		* This class is a component that is used to render a single mesh or collection of meshes. Typically,
		* they are used one per GameObject to render a static object or one per Bone(GameObject) in a Skeleton.
		*
		* @see IComponent
		* @see MeshProcess
		* @see DX11Director
		* @see Material
		* @see ConstantBufferGroup
		* @see LightingInfo
		*
		* @author Bert Wierenga
		*/
		class MeshRenderer : public IComponent
		{
			ComponentH(Hogshead::DX11Rendering::MeshRenderer, MeshRenderer)
		friend class MeshProcess;
		friend class ShadowMapGenerationProcess;
		friend class DX11Director;
		friend class AnimatedMeshRenderer;
		friend class ParticleSystem;
		public:

			static int ID;

			int _my_id;
			bool _taken;

			MeshRenderer(void);
			~MeshRenderer(void);

			void initialize();
			IComponent* readXML(const String& in_xml);
			

			/**
			Virtual method which is called when the component is changed to active
			**/
			virtual void onSetActive();
			/**
			Virtual method which is called when the component is changed to inactive
			**/
			virtual void onSetInactive();

			/**
			* @return Returns whether the current technique we are using requires lighting.
			*/
			bool useLighting() const;

			/**
			*/
			Technique* getTechnique(int process_handle);

			/**
			* @return Returns a constant reference to the ConstantBufferGroup this MeshRenderer uses. 
			* This is read in from XML and created.
			*/
			const ConstantBufferGroup& constantBufferGroup() const;
			ConstantBufferGroup& constantBufferGroup();


			const MeshFilter* meshFilter() const;						

			void setName(String in_name){this->_name = in_name;}
			String getName() const {return _name;}
			MeshFilter* getMeshFilter() const {return _meshes;}
			float getSpecularExponent() const {return _specular_exponent;}

			IProcessInfo* _process_specific_data;

			void setNeverCull(bool in_never_cull){_never_cull = in_never_cull;}

			bool neverCull() {return _never_cull;}

			bool _particle_system;

			bool getNeverCull() const
			{
				return _never_cull;
			}

			bool _registered;
		private:

			bool _never_cull;

			

			DX11Director* _dx11_director;
			

			float _specular_exponent;
			String _name;

			void setMeshFilter(MeshFilter* in_mf);
			void setConstantBufferGroup(ConstantBufferGroup in_cbg);

			static void handle_constant_buffer_modifier_message(IMessageReceiver*, IMessage*);
			//used to keep track of whether we ever need lighting or not
			bool _needs_lighting;

			//Mesh filter is a list of vertex buffers, or a list of meshes conceptually
			MeshFilter* _meshes;

			//these are stored in the order they are used
			//Vector<Pair<Technique*, String>> _techniques;
			HashTable<int, Technique*> _techniques;

			//any data we will need to send to the shaders
			ConstantBufferGroup _constant_buffer_group;

		};

	}
}